package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
@Transactional
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public List<ShoppingCart> showShoppingCart() {
        // 这里需要根据当前登录用户查询购物车

        // 暂时返回空列表或测试数据

        return shoppingCartMapper.list(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());
    }

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
  //判断当前加入购物车是否已经存在
        //根据用户id查询购物车中是否已经存在该商品
        ShoppingCart shippingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shippingCart);
        // 1. 获取当前用户ID
        Long userId = BaseContext.getCurrentId();
        if (userId == null) {
            userId = 1L;
            BaseContext.setCurrentId(userId);
            log.warn("用户ID为空，使用临时用户ID: {}", userId);
//            throw new BaseException("用户未登录");
        }

        log.info("当前用户ID: {}", userId);
        shippingCart.setUserId(userId);
//        shippingCart.setDishId(shoppingCartDTO.getDishId());
//        shippingCart.setSetmealId(shoppingCartDTO.getSetmealId());
//        shippingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
        //根据查询结果判断是否存在
        List<ShoppingCart> list = shoppingCartMapper.listByUserId(shippingCart);
        if(list != null && list.size() > 0){
            //存在，更新数量
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.update(cart);
        }else{
            //不存在，新增
            //根据dishId查询菜品信息
            if(shoppingCartDTO.getDishId() != null){
                Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
                shippingCart.setName(dish.getName());
                shippingCart.setAmount(dish.getPrice());
                shippingCart.setImage(dish.getImage());
                shippingCart.setNumber(1);
                shippingCart.setCreateTime(LocalDateTime.now());
            }else{
                //根据setmealId查询套餐信息
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = (Setmeal) setmealMapper.getDishItemBySetmealId(setmealId);
                shippingCart.setName(setmeal.getName());
                shippingCart.setAmount(setmeal.getPrice());
                shippingCart.setImage(setmeal.getImage());
                shippingCart.setNumber(1);
                shippingCart.setCreateTime(LocalDateTime.now());
            }
            shoppingCartMapper.insert(shippingCart);
        }
    }

    @Override
    public void cleanShoppingCart() {
        // 实现清空购物车逻辑
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteAll(userId);
    }
}